package com.lizy.controller;

import com.alibaba.fastjson.JSON;
import com.lizy.pojo.Roles;
import com.lizy.pojo.Users;
import com.lizy.pojo.vo.UserVo;
import com.lizy.service.RolesService;
import com.lizy.service.UsersService;
import com.lizy.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: M
 * @Date: 2024/9/2 22:34:40
 * @Version: v1.0.0
 **/
@RestController
@RequestMapping("/rbac")
public class RbacController {

    @Autowired
    UsersService userService;
    @Autowired
    RolesService rolesService;

    /**
     * @param user
     * @return @return void
     * @Author
     * @Description //TODO 新增
     **/
    @PostMapping("/insertUser")
    public void insertUser(@RequestBody Users user) {
        userService.insertUser(user);
    }

    /**
     * @param userId
     * @return @return void
     * @Author
     * @Description //TODO 根据ids删除对应的
     **/
    @DeleteMapping("/deleteUser/{userId}")
    public void deleteUser(@PathVariable("userId") Integer userId) {
        userService.deleteUser(userId);
    }

    /**
     * @param users
     * @return @return void
     * @Author
     * @Description //TODO 修改
     **/
    @PostMapping("/updateUser")
    public void updateUser(@RequestBody Users users) {
        userService.updateUser(users);
    }

    /**
     * @param roles
     * @return @return void
     * @Author
     * @Description //TODO 添加角色 给角色相应的权限
     **/
    @PutMapping("/insertRoles")
    public void insetPermissionId(@RequestBody Roles roles) {
        rolesService.insertRoles(roles);
    }


    /**
     * @param roleId
     * @return @return void
     * @Author
     * @Description //TODO 删除角色 并包含角色的权限
     **/
    @DeleteMapping("/deleteRoles/{roleId}")
    public void deleteRoles(@PathVariable("roleId") Integer roleId) {
        rolesService.deleteRoles(roleId);
    }


    /**
     * @param roles
     * @return @return void
     * @Author
     * @Description //TODO 修改角色 角色的权限
     **/
    @PostMapping("/updateByRoles")
    public void updateByRoles(@RequestBody Roles roles) {
        rolesService.updateByRoles(roles);

    }

    @PostMapping("/list")
    public void  list(@RequestBody  Roles roles){

        String s = JSON.toJSONString(roles);
        String http = HttpClientUtil.post("http://localhost:9021/rbac/updateByRoles", null, s);
        System.out.println(http);

    }


    public void updateByUser(@RequestBody UserVo userVo){
        userService.updateByUser(userVo);
    }

}
